var doc;
window.onload = function() {
	doc = document;
	init();

}

var activity = {};
var add_Dialog;

var banks = {};
var cities = {};
var categories = {};
var shops = {};
var div_shops;
var  cityId;
var selectedShops = new Array();

var shopId;
var activityId;


var  warmContent;



function addShop(val) {
	selectedShops[selectedShops.length] = val;
}
function removeShop(val) {
	for ( var i = 0; i < selectedShops.length; i++) {
		if (selectedShops[i] == val) {
			selectedShops[i] = -1;
		}
	}
}

function showAddShopDialog(id) {
	shopId = id;
	new Boxy(getShopDialog(), {
		title : "人肉匹配商户",
		closeText : "关闭",
		show : true,
		x : "0",
		center : true,
		modal : true
	});
	$('#btn_search').click(function() {
		$("#form_shops input[name='pn']").val(1);
		getShops();
	});

}

function getShopDialog() {
	$("#warm").html(warmContent);
	if (div_shops != null) {
		return div_shops;
	}
	div_shops = $("<div><div");
	div_shops.append("<div  id='warm'>"+warmContent+"</div>");

	var from_shops = $("<form id='form_shops'></form>");
	from_shops
			.append("<table width='900'><tr><td > <select name='city'></select></td><td><select name='category'></select></td><td>名称<input width='100' name='name'><input  type='hidden' name='ps' value='10'></td><td> <div  id='btn_search'>查找 </div><input  type='hidden' name='pn'></td></tr></table>");
	var table_shops = $("<table id='table_shops' width='900'></table>");
	table_shops
			.append("<tr class='head'><th width='11%'>区域</th><th  width='30%'>名称</th><th width='30%'>电话</th><th  width='30%'>地址</th></tr>");

	div_shops.append(from_shops);
	div_shops.append(table_shops);
	getCities();
	getCategories();
	getShops(1);

	return div_shops;
}

function init() {
	initBtn();
	initBtnAddShop();
	initActivity();
	
}

function initBtnAddShop() {
	var btn_add_shop = $(".btn_match_shop");
	
//	$(".btn_show_activity").click(function(){
//		var  id=$(this).parent().parent().attr('activityId');
//		url="info.htm?activityId="+id;
//		var opt={w:500,h:400};
//		if($(window).width()>500) opt.w = $(window).width()-100;
//		if($(window).height()>300) opt.h = $(window).height()-130;
//		qBox.iFrame({title:"活动商家",w:opt.w,h:opt.h,src:url});
//	})
	btn_add_shop.click(function() {
		var  tr=$(this).parent().parent();
		activityId = tr.attr('id');
		cityId=$("#"+activityId+" td")[0].id;
			warmContent=tr.find("td")[2].innerHTML+"    "+tr.find("td")[3].innerHTML;
			var  id=$(this).parent().parent().attr('activityId');
			var  name=tr.find('td')[1];
			url="./shopes.htm";
			var opt={w:500,h:400};
			if($(window).width()>500) opt.w = $(window).width()-100;
			if($(window).height()>300) opt.h = $(window).height()-130;
			qBox.iFrame({title:"活动商家",w:opt.w,h:opt.h,src:url});
	
	});  
}

function initBtn() {
	var btn_add_activity = $('.btn_match_shopws');
	btn_add_activity
			.click(function() {
				
				
				var form_activity = $("#form_activity");
				if (form_activity.length > 0)
					form_activity[0].remove();

				var form_add_activity = $("<form  id='form_activity'></form>");
				var table_add_activity = $("<table  width='900' ></table>");

				table_add_activity
						.append("<tr><td  width='100'>银行</td><td  width='100'> <select name='bank'></select></td></tr>")
				table_add_activity
						.append("<tr><td  width='100'>活动名称</td><td  width='600'><input name='activity_name' widdth='100%' type='text'></th><tr>");
				table_add_activity
						.append("<tr><td  width='100'>活动时间</th><td  width='600'><input   name='start_time' widdth='100%' type='text'>至<input name='end_time' widdth='100%' type='text'> <input type='checkbox'>周一<input type='checkbox'>周二<input type='checkbox'>周三<input type='checkbox'>周四<input type='checkbox'>周五<input type='checkbox'>周六<input type='checkbox'>周日</th><input type='checkbox'>全周</tr>");
				table_add_activity
						.append("<tr><td  width='100'>活动内容</td><td  width='600'><textarea name='activity_content' rows='10' cols='50'></textarea></th><tr>");

				form_add_activity.append(table_add_activity);

				var btn_submit = $("<div style='width:100px;background-color:#ccc'  id='submit_activity' >添加活动</div>");
				btn_submit.click(function() {
					$.ajax({
						type : "POST",
						url : "/ad/bank/add.do",
						data : $('#form_activity').serialize(),
						success : function() {
							activity.bank = $("select[name='banks']").val();
							activity.shops = "1,2,3";
							activity.name = $("input[name='activity_name']")
									.val();
							activity.time = $("input[name='start_time']").val()
									+ "至" + $("input[name='end_time']").val();
							activity.content = $(
									"textarea[name='activity_content']").val();
							addItem(activity);
						}
					});

				})

				form_add_activity.append(btn_submit);

				add_Dialog = new Boxy(form_add_activity, {
					title : "添加银行活动",
					closeText : "关闭",
					show : true,
					x : "100",
					center : true,
					modal : true
				});

				$("input[name='start_time']").datepicker({
					dateFormat : 'yy-mm-dd'
				});
				$("input[name='end_time']").datepicker({
					dateFormat : 'yy-mm-dd'
				});
				getBanks();
			});

}

function showShopsList() {
	var t = $("'<table id='shops'   class='table_list' order='0' cellspacing='0' cellpadding='0' id='idTableList'>'");
	var tr = $("<tr></tr>");
	t.append(tr);
	tr.append("<th  width='10%'>区域</th>");
	tr.append("<th  width='30%'>名称</th>");
	tr.append("<th  width='30%'>分类</th>");
	tr.append("<th  width='30%'>电话</th>");

}

function getShops() {
	$('#table_shops tr:not(.head)').remove();
	$.ajax({
		type : "POST",
		url : "/ad/wbank/getShops.do",
		data : $('#form_shops').serialize(),
		success : function(data) {
			callBack(data);
		}
	})
}

function callBack(data) {
	var result = data.findResult;
	shops = result.items;
	var items = "";
	for ( var i = 0; i < shops.length; i++) {
		
		if(shops[i].brandName==null)shops[i].brandName="";
		if(shops[i].name==null)shops[i].name="";
		if(shops[i].tels==null)shops[i].tels="";
		
		items += "<tr  id=" + shops[i].id + ">" + "<td> " + shops[i].cityName
				+ "-" + shops[i].region + "</td>" + "<td>" + shops[i].brandName
				+ "-" + shops[i].name + "<td>" + shops[i].tels + "</td>"
				+ "<td>" + shops[i].addr + "</td>" + "</tr>"
	}
	$('#table_shops tr:not(.head)').remove()
	$('#table_shops  tbody').append(items);
	$('.div_pager').remove();
	var div_pager = getPager(result.pageno, result.pagecount);
	div_pager.insertAfter("#table_shops");
	var btn_add_shops;
	$("select[name='city']").val(cityId);
	$('#table_shops tr:not(.head)').click(
			 
			function() {
				shopId = this.id;
				Boxy.confirm("亲，确定匹配该商户么？", function() {
					var url = "/ad/wbank/matchingShop.do?activityId="
							+ activityId + "&shopId=" + shopId
					$.ajax({
						type : "get",
						url : url,
						success : function(data) {
						}
					})

				},
				{
					title : 'Message'
				});
			})

	// initTableShop();
}

var checkbox;
// function initTableShop() {
// $("#table_shops tr").click(function() {
// checkbox=$("#table_shops input[name='"+this.id+"']");
// if(checkbox[0].checked==false){
// checkbox.attr("checked",true);
// addShop(this.id);
// }
// else{
// checkbox.attr("checked",false);
// removeShops(this.id);
// }
//		
//		
// });
//
// }

function getShopURL() {
	var url = "/ad/bank/getShops.do?";
	var city = $("select[name='city']").val();
	var categories = $("select[name='category']").val();
	var name = $("input[name='name']").val();
	if (city != null)
		url += "city=" + city;
	if (categories != null)
		url += "&category=" + categories;
	if (name != null)
		url += "&name=" + name;
	return url;

}

function getPager(pageNo, pageCount) {

	var begin;
	var end;
	if (pageNo - 3 < 1) {
		begin = 1;
	} else {
		begin = pageNo - 3;
	}

	end = pageNo + 3;
	if (end > pageCount) {
		end = pageCount;
	}

	var pager = $("<div  class='div_pager'></div>");
	if (pageNo > 1) {
		pager.append("<a  class='pager'  href='#'>上一页</a>");
	}
	if (begin > 1) {
		pager
				.append("<a  class='pager'  href='javascript:shopsGoto(1)' href='#'>1</a>");
		pager.append("<a  class='gap'>...</a>");
	}
	for ( var i = begin; i <= end; i++) {
		if (i != pageNo) {
			pager.append("<a  class='pager' href='javascript:shopsGoto(" + i
					+ ")'>" + i + "</a>");

		} else {
			pager.append("<a  class='pager pager_currenrt'    href='javascript:shopsGoto('"
							+ i + ")>" + i + "</a>");
		}
	}
	if (end < pageCount) {
		pager.append("<a  class='gap'>...</a>");
		pager.append("<a  class='pager' href='#'>" + pageCount + "</a>");
	}

	if (pageNo != pageCount)

		var next = parseInt(pageNo) + 1;
	pager.append("<a  class='pager'  href='javascript:shopsGoto(" + next
			+ ")' >下一页</a>");
	return pager;
}

function getBanks() {

	if (banks.length != null) {
		var options = "";
		for ( var i = 0; i < banks.length; i++) {
			options += "<option>" + banks[i].name + "</option>";
		}
		$("select[name='banks']").append(options);
		return;
	}
	$.ajax({
		type : "POST",
		url : "/ad/wbank/getBanks.do",
		success : function(data) {
			banks = data.findResult.items;
			var options = "";
			for ( var i = 0; i < banks.length; i++) {
				options += "<option value=" + banks[i]._id + " >"
						+ banks[i].name + "</option>";
			}
			$("select[name='bank']").append(options);
		}
	})
}

function getCities() {

	$.ajax({
		type : "get",
		url : "/ad/wbank/getCities.do",
		success : function(data) {
			cities = data.findResult.items;
			var options ="<option value>--所有--</option>";
			for ( var i = 0; i < cities.length; i++) {
				options += "<option value='" + cities[i].code + "'>"
						+ cities[i].name + "</option>";
			}
			$("select[name='city']").append(options);
		}
	});
}

function getCategories() {
	$.ajax({
		type : "POST",
		url : "/ad/wbank/getCategories.do",
		success : function(data) {
			console.info(data);
			categories = data.findResult.items;
			var options = "<option value>--所有--</option>";
			for ( var i = 0; i < categories.length; i++) {
				options += "<option  value='" + categories[i].code + "'>"
						+ categories[i].name + "</option>";
			}
			$("select[name='category']").append(options);
		}

	})

}

function addItem(activity) {
	var item = $("<tr id=" + 1 + "><th>" + activity.bank + "</th><th>"
			+ activity.name + "</th><th>" + activity.time + "</th><th>"
			+ activity.content + "</th>" + "<th></th>" + "<th>"
			+ "<button  name='btn_add_shops'>添加商家</button>" + "</th>" + "</tr>")
	$("#activity  tbody").append(item);
	$("button[name='btn_add_shops']").click(function() {
		var id = $(this).parent().parent().attr('id');
		showAddShopDialog(id);
	});
}

function shopsGoto(pn) {
	$("#form_shops input[name='pn']").val(pn);
	getShops();
}

function addActivityDialog() {

}

function showBoxy() {
	var mboxy = new Boxy(getShopDialog(), {
		title : "添加商户",
		closeText : "关闭",
		show : true,
		x : "0",
		center : true,
		modal : true
	});

	url = "/bank/getShops.do"
	mboxy.load(url);
}




function openShopList(url){
	var opt={w:500,h:400};
	if($(window).width()>500) opt.w = $(window).width()-100;
	if($(window).height()>300) opt.h = $(window).height()-130;
	qBox.iFrame({title:"活动商家",w:opt.w,h:opt.h,src:url});
}



function  initActivity(){   
	$(".btn_show_shops").click(function(){
		var  id=$(this).parent().parent().attr('id');
		url="activityShopes.htm?activityId="+id;
		openShopList(url);
	})
	$(".btn_show_activity").click(function(){
		var  id=$(this).parent().parent().attr('activityId');
		url="info.htm?activityId="+id;
		var opt={w:500,h:400};
		if($(window).width()>500) opt.w = $(window).width()-100;
		if($(window).height()>300) opt.h = $(window).height()-130;
		qBox.iFrame({title:"活动商家",w:opt.w,h:opt.h,src:url});
	})
}

 

